
/*
*Released on 28th June 2008.
*Any violations can be reported at paritosh@wikiocean.net
*What is treated as violations can be found at www.wikiocean.net/ppl1-voilations*
******************************************************************************
* The contents of this file are subject to POOL Public License 1.0 and later.
* POOL Public License is based on Affero GPL v3 and additional conditions.
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the Affero GPL v3 License at http://www.gnu.org/licenses/agpl.html
* You may obtain a copy of the POOL Public License 1.0 or later at www.wikiocean.net/license/ppl.html
* Software distributed under POOL Public License 1.0 is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Initial Developer of the Original Code is Paritosh Pungaliya (C) 2008. All Rights Reserved.
******************************************************************************
* Objective of the additional terms (license)
* 1) Is to extend the software freedom to freedom to work.
* 2) To ensure that knowledge is free from monopoly of agencies.
* 3) To avoid a situation where big corporate or investor can buy out free software groups and companies and then start milking the communities built around it.
* (this trend can be seen how the freedom is curtailed in companies that get Venture Capital funding.)
******************************************************************************
*/

package  validation;

import java.text.SimpleDateFormat;
import java.util.Date;

import logger.PoolLogger;
import dbmanager.DBManager;

public class Gte implements Validation
{
	String ConditionValue;
	PoolLogger pl;
	
	public Gte() {
		// logger
		pl = (PoolLogger) PoolLogger.getLogger(this.getClass().getName());
	}
/*	public boolean validate (String Value, String ConditionValue)
	{
		this.ConditionValue = ConditionValue;
		double val = Double.parseDouble(Value);
		double convalue = Double.parseDouble(ConditionValue);
		
		if (val >= convalue)
		{
			return true;
		}
		else
			return false;
	}*/
	public boolean validate (String Value, String ConditionValue)
	{
		try
		{
			this.ConditionValue = ConditionValue;
			double val = Double.parseDouble(Value);
			double convalue = Double.parseDouble(ConditionValue);
			
			if (val >= convalue)
			{
				return true;
			}
			else
				return false;
		} 
		catch (NumberFormatException e)
		{
//			STRING
			if(Value.toLowerCase().equals(ConditionValue.toLowerCase()))
				return true;
			//DATE
			else
			{
				try
				{
					if(Value.length()==ConditionValue.length())
					{
						String pattern = "yyyy-MM-dd HH:MM:ss";
						SimpleDateFormat sdm = new SimpleDateFormat(pattern);
						Date date_value;
						try 
						{
							date_value = (Date)sdm.parse(Value);
						}
						catch (Exception e1) {
							Value = Value+" 00:00:00";
							date_value = (Date)sdm.parse(Value);
						}
						
						Date date_condition;
						try
						{
							date_condition = (Date)sdm.parse(ConditionValue);
						} catch (Exception e1) {
							ConditionValue = ConditionValue+" 00:00:00";
							date_condition = (Date)sdm.parse(ConditionValue);
						}
						
						if(date_value.compareTo(date_condition)==0||date_value.compareTo(date_condition)>0)
							return true;
						else
							return false;
					}
					else
					{
						String pattern = "yyyy-MM-dd";
						SimpleDateFormat sdm = new SimpleDateFormat(pattern);
						Date date_value = (Date)sdm.parse(Value);
						Date date_condition = (Date)sdm.parse(ConditionValue);
						
						if(date_value.compareTo(date_condition)==0||date_value.compareTo(date_condition)>0)
							return true;
						else
							return false;
					}
				}
				catch (Exception e1) 
				{
					e.printStackTrace();
				}
			}
		}
		return false;
	}
	public boolean validate(String Value)
	{
		// TODO Auto-generated method stub
		return false;
	}
	public String getMessage() 
	{
		// TODO Auto-generated method stub
		pl.info("value must be greater or equal to "+ConditionValue);
		pl.debug("value must be greater or equal to "+ConditionValue);
		pl.warn("value must be greater or equal to "+ConditionValue);
		pl.fatal("value must be greater or equal to "+ConditionValue);
		pl.error("value must be greater or equal to "+ConditionValue);
		pl.trace("value must be greater or equal to "+ConditionValue);
		return "value must be greater or equal to "+ConditionValue;
	}
	
	public static void main (String args[])
	{
		Gte gte = new Gte();
		System.out.println("Validate : "+gte.validate("3", "100"));
		System.out.println("Validate : "+gte.validate("300", "100"));
		System.out.println("Validate : "+gte.validate("100", "100"));
	}

	public void initPool() 
	{
	}

	public boolean validate(String Value, String ConditionValue, String Refvalue) 
	{
		return validate(Value, ConditionValue);
	}

	public void setBasicvalue(String value) 
	{
		
	}
}
